package com.example.config;

import com.example.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //交换机名称
    private String fanoutExchangeName = Constants.MY_FANOUT_EXCHANGE;
    private String directExchangeName = Constants.MY_DIRECT_EXCHANGE;

    //队列名称
    private String smsQueueName = Constants.MY_SMS_QUEUE_NAME;
    private String emailQueueName = Constants.MY_EMAIL_QUEUE_NAME;

    /**
     * 定义 Fanout 广播模式的交换机
     * Bean 注入,没有指定名称,所以该 Bean 的名称为方法名 fanoutExchange
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        /**
         * 构造器可以有多个参数
         * String name                      交换机名称
         * boolean durable                  是否持久化
         * boolean autoDelete               是否自动删除
         * Map<String, Object> arguments    参数
         */
        FanoutExchange fanoutExchange = new FanoutExchange(fanoutExchangeName);
        return fanoutExchange;
    }

    @Bean
    public DirectExchange directExchange(){
        //DirectExchange directExchange = ExchangeBuilder.directExchange(directExchangeName).durable(true).build();
        DirectExchange directExchange = new DirectExchange(directExchangeName);
        return directExchange;
    }

    /**
     * 注入一个队列
     * 队列名称是 smsQueueName 的字符串
     * Bean 注入,没有指定名称,所以该 Bean 的名称为方法名 smsQueue 和 emailQueue
     * @return
     */
    @Bean
    public Queue smsQueue(){
        Queue queue = new Queue(smsQueueName);
        return queue;
    }

    @Bean
    public Queue emailQueue(){
        Queue queue = new Queue(emailQueueName);
        return queue;
    }


    /**
     * 将队列绑定到交换机
     * @param smsQueue  队列 Bean,就是上面的队列声明的 方法名 smsQueue 和 emailQueue
     * @param fanoutExchange 交换机
     * @return
     */
    @Bean
    public Binding binding0(Queue smsQueue,FanoutExchange fanoutExchange){
        //将队列绑定到交换机
        Binding binding = BindingBuilder.bind(smsQueue).to(fanoutExchange);
        return binding;
    }

    @Bean
    public Binding binding1(Queue emailQueue,FanoutExchange fanoutExchange){
        Binding binding = BindingBuilder.bind(emailQueue).to(fanoutExchange);
        return binding;
    }


    @Bean
    public Binding binding2(Queue smsQueue,DirectExchange directExchange){
        //将队列绑定到交换机
        Binding binding = BindingBuilder.bind(smsQueue).to(directExchange).with("sms");
        return binding;
    }

    @Bean
    public Binding binding3(Queue emailQueue,DirectExchange directExchange){
        Binding binding = BindingBuilder.bind(emailQueue).to(directExchange).with("email");
        return binding;
    }



}